//
//  ForgetPwdVC.m
//  sensen_ios
//
//  Created by pro on 16/2/1.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ForgetPwdVC.h"
#import "UserOperationView.h"
@interface ForgetPwdVC ()

@property (nonatomic, strong) UserOperationView *userView;

@end

@implementation ForgetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"重置密码";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createLeftBarButtonItemWithImagename:nil]; // 左侧返回按钮
    
    [self _createViews]; // 创建视图
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // 设置导航栏颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
}
#pragma mark - 创建视图
- (void)_createViews {
    
    [self _createUserView];
    [self _createButtons];
}
// 创建用户操作视图（登陆，注册等）
- (void)_createUserView {
    
    _userView = [[UserOperationView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, lineHeight * 4)
                                                  titles:@[@"手机号", @"请输入验证码", @"请输入新的密码", @"确认新密码"]
                                                    type:ZKUserViewForgetPwd];
    [self.scrollView addSubview:_userView];
}

- (void)_createButtons {
    CGFloat x = 20;
    CGFloat y = _userView.bottom + 20;
    CGFloat width = SCREEN_WIDTH - 2 * x;
    CGFloat height = 40;
    
    // 登录按钮
    UIButton *okButton = [UIButton buttonWithType:UIButtonTypeCustom];
    okButton.frame = CGRectMake(x, y, width, height);
    okButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    [okButton setTitle:@"确认" forState:UIControlStateNormal];
    [okButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [okButton addTarget:self action:@selector(okAction:) forControlEvents:UIControlEventTouchUpInside];
    okButton.layer.cornerRadius = 2;
    [self.scrollView addSubview:okButton];
    okButton.tag = 99;
    okButton.enabled = NO;
}

#pragma mark - 点击事件
- (void)okAction:(UIButton *)button {
    // 点击注册后获得用userView中获取来的数据
    [_userView getForgetPwdInfo:^(NSDictionary *infoDic) {
        
        NSMutableDictionary *userDic = [NSMutableDictionary dictionary];
        [userDic setObject:infoDic[@"mobile"] forKey:@"mobile"];
        [userDic setObject:infoDic[@"password"] forKey:@"psw"];
        [userDic setObject:infoDic[@"code"] forKey:@"code"];
        
        [BoyeHttpClient post:@"BY_User_findPswByMobile" apiVerson:100 params:userDic success:^(AFHTTPRequestOperation *operation, id result) {
            NSLog(@"%@", result);
            [SVProgressHUD showSuccessWithStatus:@"重置密码成功" withDuration:1];
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error.domain] withDuration:1];

        }];
    }];
}

#pragma mark - 其他
// 输入框内容改变通知事件
- (void)allTextFieldEndEditing {
    UIButton *okButton = [self.view viewWithTag:99];
    if ([_userView isAllTextFieldEdited]) {
        okButton.backgroundColor = SS_NAVI_ORANGE_COLOR;
        okButton.enabled = YES;
    } else {
        okButton.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
        okButton.enabled = NO;
    }
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
